package cn.yuanqiao.archive.bianyan.service.Impl;

import cn.yuanqiao.archive.appManager.service.ITenantSysConfigService;
import cn.yuanqiao.archive.bianyan.domain.YqDataGerenKu;
import cn.yuanqiao.archive.bianyan.domain.YqDataZhuantiGern;
import cn.yuanqiao.archive.bianyan.mapper.YqDataGerenKuMapper;
import cn.yuanqiao.archive.bianyan.mapper.YqDataZhuantiGernMapper;
import cn.yuanqiao.archive.bianyan.service.IYqDataGerenKuService;
import cn.yuanqiao.common.constant.Constants;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 资料资源个人库Service业务层处理
 *
 * @ProjectName: ruoyi-modules-system
 * @Package: cn.yuanqiao.archive.bianyan.service.Impl
 * @ClassName: YqDataGerenKuServiceImpl
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/7/5 16:28
 * @Version: 1.0
 */
@Service
public class YqDataGerenKuServiceImpl implements IYqDataGerenKuService {
    @Autowired
    private YqDataGerenKuMapper yqDataGerenKuMapper;

    @Autowired
    private YqDataZhuantiGernMapper yqDataZhuantiGernMapper;

    @Autowired
    private ITenantSysConfigService sysConfigService;

    /**
     * 查询资料资源个人库
     *
     * @param ID 资料资源个人库主键
     * @return 资料资源个人库
     */
    @Override
    public YqDataGerenKu selectYqDataGerenKuByID(String ID) {
        String tableCode = SecurityUtils.getTenantCode();
        return yqDataGerenKuMapper.selectYqDataGerenKuByID(ID, tableCode + Constants.TABLE_YQ_DATA_GEREN_KU);
    }

    /**
     * 查询资料资源个人库列表
     *
     * @param yqDataGerenKu 资料资源个人库
     * @return 资料资源个人库
     */
    @Override
    public List<YqDataGerenKu> selectYqDataGerenKuList(YqDataGerenKu yqDataGerenKu) {
        String tableName = SecurityUtils.getTenantCode() + Constants.TABLE_YQ_DATA_GEREN_KU;
        return yqDataGerenKuMapper.selectYqDataGerenKuList(yqDataGerenKu, tableName);
    }

    /**
     * 新增资料资源个人库
     *
     * @param yqDataGerenKu 资料资源个人库
     * @return 结果
     */
    @Override
    public int insertYqDataGerenKu(YqDataGerenKu yqDataGerenKu) {
        yqDataGerenKu.setCreateTime(DateUtils.getNowDate());
        yqDataGerenKu.setId(String.valueOf(SnowflakeIdGenerator.generateId()));
        yqDataGerenKu.setIsDel("0");
        yqDataGerenKu.setCreator(SecurityUtils.getUsername());
        yqDataGerenKu.setCreatorId(String.valueOf(SecurityUtils.getUserId()));
        //获取数据库表
        String tableName = SecurityUtils.getTenantCode() + Constants.TABLE_YQ_DATA_GEREN_KU;
        return yqDataGerenKuMapper.insertYqDataGerenKu(yqDataGerenKu, tableName);
    }

    /**
     * 修改资料资源个人库
     *
     * @param yqDataGerenKu 资料资源个人库
     * @return 结果
     */
    @Override
    public int updateYqDataGerenKu(YqDataGerenKu yqDataGerenKu) {
        //  SecurityUtils.getTenantCode()
        String tableName = SecurityUtils.getTenantCode() + Constants.TABLE_YQ_DATA_GEREN_KU;
        return yqDataGerenKuMapper.updateYqDataGerenKu(yqDataGerenKu, tableName);
    }

    /**
     * 批量删除资料资源个人库
     *
     * @param ids 需要删除的资料资源个人库主键
     * @return 结果
     */
    @Override
    public int deleteYqDataGerenKuByIDs(String ids) {
        // SecurityUtils.getTenantCode()
        String tableName = SecurityUtils.getTenantCode() + Constants.TABLE_YQ_DATA_GEREN_KU;
//        List<YqDataGerenKu> yqDataGerenKus = yqDataGerenKuMapper.selectYqDataGerenKuByIds(ids.split(","), tableName);
//        String uplodPath = sysConfigService.selectConfigByKey("YQ_MNT");
//        for (YqDataGerenKu da : yqDataGerenKus) {
//            File file = new File(uplodPath + da.getFilePath());
//            int suffix = da.getFilePath().lastIndexOf(".");
//            if (suffix < 0) {
//                continue;
//            }
//            if (file.exists()) {
//                //如果文件存在就删除文件
//                file.delete();
//            }
//        }
        return yqDataGerenKuMapper.deleteYqDataGerenKuByIDs(ids.split(","), tableName);
    }

    /**
     * 删除资料资源个人库信息
     *
     * @param ID 资料资源个人库主键
     * @return 结果
     */
    @Override
    public int deleteYqDataGerenKuByID(String ID) {
        String tableName = SecurityUtils.getTenantCode() + Constants.TABLE_YQ_DATA_GEREN_KU;
        return yqDataGerenKuMapper.deleteYqDataGerenKuByID(ID, tableName);
    }

    /**
     * 根据专题id 查询专题列表
     *
     * @param yqDataGerenKu
     * @return
     */
    @Override
    public List<YqDataGerenKu> selectYqZhuanTiList(YqDataGerenKu yqDataGerenKu) {
        //SecurityUtils.getTenantCode()
        String tableCode = SecurityUtils.getTenantCode();
        String tableName = tableCode + Constants.TABLE_YQ_DATA_GEREN_KU;
        String tableNameTp = tableCode + Constants.TABLE_YQ_ZHUANTI_GEREN_DATA;

        return yqDataGerenKuMapper.selectYqZhuanTiList(yqDataGerenKu, tableName, tableNameTp);
    }

    /**
     * 添加至专题库
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public int insertYqZhuanTiList(List<YqDataZhuantiGern> yqDataZhuantiGern) {
        //获取表名信息 SecurityUtils.getTenantCode()
        String tableCode = SecurityUtils.getTenantCode();
        String tableName = tableCode + Constants.TABLE_YQ_DATA_GEREN_KU;
        String tableNameTp = tableCode + Constants.TABLE_YQ_ZHUANTI_GEREN_DATA;

        String gerenIds = yqDataZhuantiGern.stream().map(m -> m.getGerenId()).collect(Collectors.joining(","));
        //修改个人库状态
        yqDataGerenKuMapper.updateYqDataGerenKuId(gerenIds.split(","), "2", tableName);
        return yqDataZhuantiGernMapper.insertYqDataZhuantiGern(yqDataZhuantiGern, tableNameTp);
    }

    @Override
    @Transactional
    public int deleteYqZhuanTiByIDs(String[] gerenIds) {
        //获取表名信息SecurityUtils.getTenantCode()
        String tableCode = SecurityUtils.getTenantCode();
        String tableName = tableCode + Constants.TABLE_YQ_DATA_GEREN_KU;
        String tableNameTp = tableCode + Constants.TABLE_YQ_ZHUANTI_GEREN_DATA;

        yqDataZhuantiGernMapper.deleteYqDataZhuantiGernByGerenIds(gerenIds, tableNameTp);
        return yqDataGerenKuMapper.updateYqDataGerenKuId(gerenIds, "1", tableName);
    }

    /**
     * 移除和添加至公共库
     *
     * @param gerenIds
     * @return
     */
    @Override
    public int communalUpdate(String[] gerenIds, String typeState) {
        //SecurityUtils.getTenantCode()
        String tableCode = SecurityUtils.getTenantCode();
        String tableName = tableCode + Constants.TABLE_YQ_DATA_GEREN_KU;
        return yqDataGerenKuMapper.updateYqDataGerenKuId(gerenIds, typeState, tableName);
    }
}
